package kava.framework.demo;

import javax.microedition.lcdui.Graphics;

import kava.framework.lcdui.ContainerForm;
import kava.framework.lcdui.KavaFrame;
import kava.framework.lcdui.ScriptParser;

public class MsgBox extends ContainerForm {
	private boolean autoDismiss;
	private boolean comfirmType;
	private String text;

	public MsgBox(boolean isAutoDismiss, boolean isComfirm, String text) {
		ScriptParser.parseFormFromScript(this, "/ui.txt", "msgbox");
		this.autoDismiss = isAutoDismiss;
		this.comfirmType = isComfirm;
		this.text = text;
		
		if(this.comfirmType){
			this.getChildByID(1).visual = true;
			this.getChildByID(2).visual = true;
		}
	}

	protected void onWidgetPressed(int widgetID) {
		// TODO Auto-generated method stub
		switch (widgetID) {
		case 1:
			// yes
			DemoMIDlet.exit();
			break;
		case 2:
			KavaFrame.pop();
			break;

		}
	}

	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		g.setColor(this.fgColor);
		g.drawString(text, x + this.width / 2, this.y + 10, Graphics.TOP
				| Graphics.HCENTER);

	}

	protected void onKeyPressed(int keyCode) {
		if (keyCode == KavaFrame.KEYCODE_LEFT
				|| keyCode == KavaFrame.KEYCODE_RIGHT) {
			int fid = this.getFocusChildID();
			int newFid = fid == 1 ? 2 : 1;
			this.setFocusChildByID(newFid);
			this.updateForm();
		}else if(keyCode == KavaFrame.KEYCODE_FIRE){
			int fid = this.getFocusChildID();
			onWidgetPressed(fid);
		}
		
	}

	protected void onActive() {
		// TODO Auto-generated method stub
		super.onActive();
		if(this.autoDismiss){
			this.setTick(System.currentTimeMillis());
			KavaFrame.addAppMessage(DemoMIDlet.APP_MSG_CHECK_MSGBOX_LIVE, null);
		}
	}



}
